package net.htjs.juc.chapter06.atomicreference;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReference;

@Slf4j
public class AtomicAba {

    static AtomicReference<String> ref = new AtomicReference<>("A");

    public static void main(String[] args) throws InterruptedException {
        log.info("main start");
        String prev = ref.get();
        other();
        Thread.sleep(1000);
        log.info("change A -> C {}", ref.compareAndSet("A", "C"));
    }

    private static void other() throws InterruptedException {
        new Thread(() -> {
            log.info("thread1 start");
            log.info("change A -> B {}", ref.compareAndSet("A", "B"));
        }).start();
        Thread.sleep(500);
        new Thread(() -> {
            log.info("thread2 start");
            log.info("change B -> A {}", ref.compareAndSet("B", "A"));
        }).start();
    }


}
